<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="listQuery"
            class="demo-form-inline"
          >
            <el-form-item>
              <el-select
                filterable
                v-model="listQuery.type"
                placeholder="默认为聊天室记录"
              >
                <el-option
                  v-for="item in chatType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-date-picker
                v-model="listQuery.date_start"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="创建开始时间"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="">
              <el-date-picker
                v-model="listQuery.date_end"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="创建结束时间"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="" v-if="this.listQuery.type === 1">
              <el-input
                type="text"
                v-model="listQuery.room_name"
                placeholder="请输入房间名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.content"
                placeholder="请输入聊天内容"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="请输入昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.user_code"
                placeholder="请输入用户编号"
              ></el-input>
            </el-form-item>
            <el-form-item label="" v-if="this.listQuery.type === 2">
              <el-input
                type="text"
                v-model="listQuery.target_nickname"
                placeholder="请输入对方昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="" v-if="this.listQuery.type === 2">
              <el-input
                type="text"
                v-model="listQuery.target_user_code"
                placeholder="请输入对方用户编号"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
              <el-button type="primary" @click="exportExcel"
                >导出excel</el-button
              >
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table
          ref="dataList"
          :data="menuList"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column prop="user_code" label="用户ID"></el-table-column>
          <el-table-column prop="nickname" label="昵称"> </el-table-column>
          <el-table-column prop="head_picture" label="头像">
            <template slot-scope="scope">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img
                  :src="scope.row.head_picture"
                  alt=""
                  style="max-width: 200px;max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.head_picture"
                  alt=""
                  style="width: 50px;"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            v-if="tableType === 2"
            prop="target_user_code"
            label="对方ID"
          ></el-table-column>
          <el-table-column
            v-if="tableType === 2"
            prop="target_nickname"
            label="对方昵称"
          >
          </el-table-column>
          <el-table-column
            v-if="tableType === 2"
            prop="target_head_picture"
            label="对方头像"
          >
            <template slot-scope="scope">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img
                  :src="scope.row.target_head_picture"
                  alt=""
                  style="max-width: 200px;max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="scope.row.target_head_picture"
                  alt=""
                  style="width: 50px;"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            v-if="tableType === 1"
            prop="room_name"
            label="房间名称"
          ></el-table-column>
          <el-table-column prop="content" label="消息内容"></el-table-column>
          <el-table-column prop="chat_time" label="消息时间"></el-table-column>

          <!-- <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)" plain>编辑</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除</el-button>
              </template>
          </el-table-column> -->
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style:normal;color:red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog
      v-dialogDrag
      :title="titleMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      @close="handleDialogClose('editRulesForm')"
    >
      <el-form
        :model="editRulesForm"
        :rules="editRulesFormRules"
        ref="editRulesForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="模块说明" prop="module_explain">
          <el-input
            v-model="editRulesForm.module_explain"
            placeholder="请输入模块名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户经验等级" prop="user_level">
          <el-input
            v-model="editRulesForm.user_level"
            placeholder="请输入此模块需要的用户经验等级"
          ></el-input>
        </el-form-item>
        <el-form-item label="黑名单类别" prop="blackList_type">
          <el-input
            v-model="editRulesForm.blackList_type"
            placeholder="请输入所属黑名单类别"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="editRulesForm.status" label="0">开启</el-radio>
          <el-radio v-model="editRulesForm.status" label="1">关闭</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listData, exportExcel } from "@/api/content/chatLog";

export default {
  data() {
    return {
      tableType: 1,
      chatType: [
        { value: 1, label: "聊天室记录" },
        { value: 2, label: "个人记录" }
      ],
      // 黑名单类别
      blackList: [
        { value: 1, label: "1" },
        { value: 2, label: "4" },
        { value: 3, label: "6" },
        { value: 4, label: "3" },
        { value: 5, label: "5" }
      ],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        module_explain: [
          { required: true, message: "请输入模块名称", trigger: "blur" }
        ],
        user_level: [
          {
            type: "number",
            required: true,
            message: "请输入正确的用户经验等级",
            trigger: "blur"
          }
        ],
        blackList_type: [
          { required: true, message: "请输入正确的黑名单类别", trigger: "blur" }
        ]
      },
      // 受限类型
      limit_typeList: [
        { value: 1, label: "全部" },
        { value: 2, label: "IP" },
        { value: 3, label: "归属地" }
      ],
      editRulesForm: {
        module_explain: undefined,
        user_level: undefined,
        blackList_type: undefined,
        status: "1"
      },
      dialogStatus: "",
      // dialog的头部信息
      titleMap: {
        update: "编辑",
        create: "添加"
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
        type: 1,
        keyword: undefined,
        date_start: undefined,
        date_end: undefined,
        content: undefined,
        room_name: undefined,
        nickname: undefined,
        user_code: undefined,
        target_nickname: undefined,
        target_user_code: undefined
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      listData(this.listQuery).then(({ data: { data, total } }) => {
        this.menuList = data;
        this.total = total;
        this.tableType = this.listQuery.type;
        this.listLoading = false;
      });
    },
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 点击重置
    resetSubmit() {
      this.listQuery = {
        page: 1,
        perPage: 10,
        type: 1,
        keyword: undefined,
        date_start: undefined,
        date_end: undefined,
        content: undefined,
        room_name: undefined,
        nickname: undefined,
        user_code: undefined,
        target_nickname: undefined,
        target_user_code: undefined
      };
    },
    exportExcel() {
      let urlData = "?";
      /* if (this.listQuery.page) {
          urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
          urlData = urlData + `perPage=${this.listQuery.perPage}`;
      } */
      if (this.listQuery.type) {
        urlData = urlData + `type=${this.listQuery.type}`;
      }
      if (this.listQuery.keyword) {
        urlData = urlData + `&keyword=${this.listQuery.keyword}`;
      }
      if (this.listQuery.date_start) {
        urlData = urlData + `&date_start=${this.listQuery.date_start}`;
      }
      if (this.listQuery.date_end) {
        urlData = urlData + `&date_end=${this.listQuery.date_end}`;
      }
      if (this.listQuery.room_name) {
        urlData = urlData + `&room_name=${this.listQuery.room_name}`;
      }
      if (this.listQuery.content) {
        urlData = urlData + `&content=${this.listQuery.content}`;
      }
      if (this.listQuery.nickname) {
        urlData = urlData + `&nickname=${this.listQuery.nickname}`;
      }
      if (this.listQuery.user_code) {
        urlData = urlData + `&user_code=${this.listQuery.user_code}`;
      }
      if (this.listQuery.target_nickname) {
        urlData =
          urlData + `&target_nickname=${this.listQuery.target_nickname}`;
      }
      if (this.listQuery.target_user_code) {
        urlData =
          urlData + `&target_user_code=${this.listQuery.target_user_code}`;
      }

      exportExcel(urlData).then(response => {
        location.href = response.data.download_url;
      });
    },
    // 点击新增
    handleCreate() {
      this.resetForm("editRulesForm");
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    // 点击编辑
    handleUpdate(row) {
      this.editRulesForm.module_explain = row.module_explain;
      this.editRulesForm.user_level = row.user_level;
      this.editRulesForm.blackList_type = row.blackList_type;
      this.editRulesForm.status = row.status;

      this.dialogStatus = "update";
      this.dialogFormVisible = true;
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields();
        }
        this.editRulesForm = {
          module_explain: undefined,
          user_level: undefined,
          blackList_type: undefined,
          status: "0"
        };
      });
    },
    // 获取省份
    getProvince() {},
    // 获取城市
    getCity() {},
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert('submit!');
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选要删除的内容",
          type: "warning"
        });
        return;
      }
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      if (this.multipleSelection.length !== 0) {
        let ids = "";
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`;
        }
        ids = ids.substring(0, ids.length - 1);
        this.dataIds = { ids: ids };
      }
      // this.dataIds)
      this.deleteDialogVisible = false;
      /* postDeleteRoom(this.dataIds).then((response) => {
          if (response.code === 200) {
              this.getList()
              this.deleteDialogVisible = false;
              this.$notify({
                  type:'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
              });
          } else {
              this.$message({
                  message: response.message,
                  type: 'error'
              });
          }
      }) */
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
      }
    }
  }
  // watch: {
  //   "listQuery.type"(value) {
  //     this.tableType = value
  //   }
  // }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

::v-deep.el-card__header {
  padding-bottom: 0;
}
</style>
